package com.lhj.bootdemo.thread;

import lombok.SneakyThrows;

/**
 * @author lhj
 * @version 1.0
 * @date 2022/4/30 9:45 上午
 * @description: 怎么停止线程的方法
 * (1)通过标记位停止
 * (2)通过interrupted()给线程设置清除标记方式停止
 * (3)通过stop方法停止，jdk过时方法，不推荐使用，有线程安全问题
 */

public class InterruptedThreadDemo implements Runnable {


    @SneakyThrows
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
                Thread.sleep(1000);
            System.out.println("运行中");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        InterruptedThreadDemo interruptedThreadDemo = new InterruptedThreadDemo();
        Thread thread = new Thread(interruptedThreadDemo);
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }
}
